<template>
    <div id="app">
        <h1>CSV 文件生成器</h1>
        <button @click="generateCSV">生成并下载 CSV 文件</button>
    </div>
</template>

<script>
export default {
    name: 'App',
    methods: {
        generateCSV() {
            const startTime = new Date('2023-10-01T00:00:00.000Z')
            const endTime = new Date('2023-10-01T23:59:59.000Z')

            // 生成表头
            let csvContent = '时间'
            for (let i = 1; i <= 3; i++) {
                csvContent += `,线${i}`
            }
            csvContent += '\n'

            // 生成数据
            let currentDate = new Date(startTime)
            while (currentDate <= endTime) {
                // 添加时间戳
                csvContent += currentDate.toISOString() + ','

                // 生成线1到线50的随机数据
                for (let i = 1; i <= 3; i++) {
                    let randomValue
                    if (i === 1) {
                        randomValue = Math.floor(Math.random() * 10) + 1 // 1-10
                    } else if (i === 2) {
                        randomValue = Math.floor(Math.random() * 901) + 100 // 100-1000
                    } else {
                        randomValue = Math.floor(Math.random() * 1001) + 9000 // 9000-10000
                    }
                    csvContent += randomValue + (i < 3 ? ',' : '\n')
                }

                // 时间加一秒
                currentDate.setSeconds(currentDate.getSeconds() + 1)
            }

            // 触发下载
            const blob = new Blob([csvContent], {
                type: 'text/csv;charset=utf-8;',
            })
            const link = document.createElement('a')
            if (link.download !== undefined) {
                const url = URL.createObjectURL(blob)
                link.setAttribute('href', url)
                link.setAttribute('download', '3列数据.csv')
                link.style.visibility = 'hidden'
                document.body.appendChild(link)
                link.click()
                document.body.removeChild(link)
            }
        },
    },
}
</script>

<style scoped>
#app {
    text-align: center;
    margin-top: 50px;
}
button {
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
}
</style>
